﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.InteropServices;
using WhiteBoard;

namespace WFFS
{
    public partial class FrmUnit : Form
    {
        private Form1 frmMain = null;       
        public FrmUnit(Form1 _frm)
        {
            InitializeComponent();           
            frmMain = _frm;
        }
        private void FrmUnit_Load(object sender, EventArgs e)
        {
            LVUnit.Clear();
            LVBuilding.Clear();
            LVFloor.Clear();

            LVUnit.BeginUpdate();
            #region 增加列名
            LVUnit.View = View.Details;
            LVUnit.Columns.Add("单位名称", 120);
            //LVUnit.Columns.Add("单位地址", 120);
            #endregion

            string sql1 = "select distinct UnitName,Address from UnitInfo ";
            SqlDataReader reader1 = null;
            SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
            reader1 = cmd1.ExecuteReader();

            while (reader1.Read())
            {
                //string unitid = reader1["UnitID"].ToString();
                string name = reader1["UnitName"].ToString();
                //string address = reader1["Address"].ToString();

                LVUnit.Items.Add(name, name, 0);
                //LVUnit.Items[name].SubItems.Add(address);
            }
            reader1.Close();
            LVUnit.EndUpdate();

        }
        private void LVUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = "";
            if (this.LVUnit.FocusedItem != null)
            {
                name = this.LVUnit.FocusedItem.SubItems[0].Text;
                LVBuilding.Clear();
                LVFloor.Clear();

                LVBuilding.BeginUpdate();
                #region 增加列名
                LVBuilding.View = View.Details;
                LVBuilding.Columns.Add("楼栋", 60);
                #endregion

                string sql1 = "select distinct UnitName,Building from UnitInfo where UnitName='" + name + "'";
                SqlDataReader reader1 = null;
                SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
                reader1 = cmd1.ExecuteReader();

                while (reader1.Read())
                {
                    string uName = reader1["UnitName"].ToString();
                    string building = reader1["Building"].ToString();
                    LVBuilding.Items.Add(uName, building, 0);
                }
                reader1.Close();
                LVBuilding.EndUpdate();
            }
        }

        private void LVBuilding_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = "";
            string buliding = "";
            if (this.LVBuilding.FocusedItem != null)
            {
                name = this.LVBuilding.FocusedItem.SubItems[0].Name;
                buliding = this.LVBuilding.FocusedItem.SubItems[0].Text;
                LVFloor.Clear();

                LVFloor.BeginUpdate();
                #region 增加列名
                LVFloor.View = View.Details;
                LVFloor.Columns.Add("楼栋", 60);

                #endregion

                string sql1 = "select distinct UnitName,Building,Floor,PicAdd from UnitInfo where UnitName='" + name + "' and Building='" + buliding + "'";
                SqlDataReader reader1 = null;
                SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
                reader1 = cmd1.ExecuteReader();

                while (reader1.Read())
                {
                    string uName = reader1["UnitName"].ToString();
                    string building = reader1["Building"].ToString();
                    string floor = reader1["Floor"].ToString();
                    string picadd = reader1["PicAdd"].ToString();
                    LVFloor.Items.Add(uName + ',' + building, floor, 0);
                }
                reader1.Close();
                LVFloor.EndUpdate();
            }
        }
        private void LVFloor_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = this.LVFloor.FocusedItem.SubItems[0].Name;
            string floor = this.LVFloor.FocusedItem.SubItems[0].Text;
            string[] arr = name.Split(',');

            string sql1 = "select distinct UnitName,picAdd from UnitInfo where UnitName='" + arr[0] + "' and Building='" + arr[1] + "' and Floor='" + floor + "'";
            SqlDataReader reader1 = null;
            SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
            reader1 = cmd1.ExecuteReader();

            while (reader1.Read())
            {
                string address = reader1["picAdd"].ToString();
                //设置图片
                WBMsgDrawingImage wBMsgDrawingImage = new WBMsgDrawingImage();
                wBMsgDrawingImage.type = "path";
                wBMsgDrawingImage.path = address;
                frmMain.setImage(wBMsgDrawingImage);
            }

            reader1.Close();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            LVUnit.Clear();
            LVBuilding.Clear();
            LVFloor.Clear();

            LVUnit.BeginUpdate();
            #region 增加列名
            LVUnit.View = View.Details;
            LVUnit.Columns.Add("单位名称", 80);
            //LVUnit.Columns.Add("单位地址", 120);
            #endregion

            string sql1 = "select distinct UnitName,Address from UnitInfo where UnitName like '%" + tbUnitName.Text + "%'";
            SqlDataReader reader1 = null;
            SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
            reader1 = cmd1.ExecuteReader();

            while (reader1.Read())
            {
                //string unitid = reader1["UnitID"].ToString();
                string name = reader1["UnitName"].ToString();
                //string address = reader1["Address"].ToString();

                LVUnit.Items.Add(name, name, 0);
                //LVUnit.Items[name].SubItems.Add(address);
            }
            reader1.Close();
            LVUnit.EndUpdate();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            tbUnitName.Text = "";
        }

        private void FrmUnit_FormClosed(object sender, FormClosedEventArgs e)
        {
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmUnit", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmUnit", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    }
}    

